package chap09;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class EchoClient {
    public static void main(String [] args) throws IOException{
        Socket echoSocket=null;
        PrintWriter out = null;//PrintWriter 作为输出流的优势是在于可以自动换行？
        BufferedReader in=null;
        try{
            echoSocket =new Socket("localhost",1111);//向server:1111发送连接握手
            out =new PrintWriter(echoSocket.getOutputStream(),true);
            in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
        }catch(UnknownHostException e){
            System.err.println("连接失败");
            System.exit(1);
        }
        System.out.println(in.readLine());
        System.out.println(in.readLine());// 为什么要重复输出两次?每一次输出 既要readline()一次 一点蠢
//        String tmp=null;
//        while((tmp=in.readLine())!=null){
//            System.out.println(tmp);
//        }
        System.out.println("输出结束");
        BufferedReader stdln = new BufferedReader(new InputStreamReader(System.in));//把输入流放入 inputReader中
        String userInput;
        //将客户端socket 输入流输出到标准输出流
        while((userInput=stdln.readLine())!=null){//这个 是请求输入？直到没什么任何输入
            out.println(userInput);//输出获取到的东西哦
             System.out.println(in.readLine());
        }
        out.close();//收尾
        in.close();
        echoSocket.close();
    }
}
